<template>
  <div style="line-height: 60px;display: flex;">
    <!-- 收缩图标· -->
    <div style="flex: 1;">
      <span :class="collapseBtnClass" style="cursor: pointer;font-size: 19px;" @click="collapse"></span>
      <el-breadcrumb separator="/" style="display:inline-block;margin-left:10px">
        <el-breadcrumb-item :to="'/'">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ currentPathName }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 收缩图标· -->
    <!-- 展示登录用户 -->
    <el-dropdown style="width: 150px; cursor: pointer;text-align:right">
      <div style="display:inline-block">
        <img :src="user.avatarUrl" style="width:30px;
                        border-radius:50%;
                        position:relative;
                        top:10px;
                        right:5px
                              " />
        <span>{{ user.nickname }}</span>
        <i class="el-icon-arrow-down" style="margin-left: 5px"></i>
      </div>

      <el-dropdown-menu slot="dropdown" style="width:100px;text-align:center">
        <el-dropdown-item>
          <a href="#" @click="person">个人信息</a>
        </el-dropdown-item>

        <el-dropdown-item>
          <span style="text-decorration:none" @click="logout">退出</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <!-- 展示个人信息 -->

    <el-drawer title="个人信息" :visible.sync="drawer" :with-header="false">
      <el-card style="width:500px;">
        <el-form label-width="100px" size="smail">
          <!-- 头像上传 -->
          <el-upload class="avatar-uploader" action="http://localhost:8888/file/upload/img" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="user.avatarUrl" :src="user.avatarUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-form-item label="用户名">
            <el-input v-model="user.username" disabled autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="user.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="user.sex" placeholder="请选择性别">
              <el-option label="男" :value="0"></el-option>
              <el-option label="女" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="user.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="user.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="user.address" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { updateUser } from '../api/admin/user/User'
export default {
  props: {
    drawer: false,
    collapseBtnClass: String,
    collapse: "",
    user: localStorage.getItem("adminInfo")
      ? JSON.parse(localStorage.getItem("adminInfo"))
      : {}
  },
  computed: {
    currentPathName() {
      return this.$store.state.currentPathName;
    }
  },
  data() {
    return {
    };
  },
  watch: {
    currentPathName(newVal, oldVal) {
      console.log(newVal);
    }
  },
  methods: {
    person() {
      this.drawer = true
    },
    handleAvatarSuccess(res) {
      this.user.avatarUrl = res;
    },
    save() {
      updateUser(this.user).then(res => {
        if (res.code === "200") {
          this.$message.success("保存成功");
          //更新浏览器存储的用户信息
          localStorage.setItem("adminInfo", JSON.stringify(this.user));
          this.drawer = false
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    logout() {
      this.$router.push("/login");
      localStorage.removeItem("adminInfo");
      this.$message.success("退出成功");
    }
  }
};
</script>

<style scoped>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}

.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>